package com.learner.ucenter.controller.api;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.common.util.JwtUtils;
import com.learner.pojo.user.ValidateMessage;
import com.learner.pojo.user.vo.ValidateMessageResponseVo;
import com.learner.ucenter.service.ValidateMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ice2020x
 * @Date: 2023/2/27
 * @Description: 验证消息控制器
 **/
@RequestMapping("/api/ucenter/validate")
@RestController
@Slf4j
public class ValidateMessageController {
    @Resource
    private ValidateMessageService validateMessageService;

    /**
     * 根据条件查询验证消息列表
     *
     * @param status       status
     * @param validateType 校验类型
     * @return ValidateMessageResponseVo 验证消息列表
     */
    @GetMapping("/getMyValidateMessageList")
    @ResponseBody
    public List<ValidateMessageResponseVo> getMyValidateMessageList(@RequestParam(value = "status", required = false) Integer status,
                                                                    @RequestParam(value = "validateType", required = false) Integer validateType,
                                                                    HttpServletRequest request) {
        String currentUserId = JwtUtils.getCurrentUserId(request);
        if (currentUserId == null) {
            throw ExceptionUtil.createException(RestException.class, 1000020);
        }
        log.info("currentUserId:{},status :{} ,validateType:{}", currentUserId, status, validateType);
        List<ValidateMessageResponseVo> validateMessageList = validateMessageService.getMyValidateMessageList(currentUserId, status, validateType);
        log.info("根据条件查询验证消息列表：{}", validateMessageList);
        return validateMessageList;
    }

    /**
     * 根据条件查询某条验证信息
     *
     * @param roomId       房间id
     * @param status       status
     * @param validateType 校验类型
     * @return ValidateMessageResponseVo 验证消息
     */
    @GetMapping("/getValidateMessage")
    public ValidateMessage getValidateMessage(String roomId, Integer status, Integer validateType) {
        ValidateMessage validateMessage = validateMessageService.findValidateMessage(roomId, status, validateType);
        log.info("根据条件查询某条验证信息:{}", validateMessage);
        return validateMessage;
    }
}
